<?php

namespace App\Http\Controllers\admin;

use App\Http\Model\Brand;
use App\Http\Model\Goods;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class BrandController extends AdminPlatformController
{
    /**
     * 品牌列表展示
     * @return  object;
     */
    public function brand_list(){
            $data = Brand::orderBy('brand_id','asc')->paginate(1);
            return view('admin.brand.brandList',compact('data'));
    }

    /**
     * 品牌添加列表
     * @return  列表视图;
     */
    public function brand_add(){
        return view('admin.brand.brandAdd');
    }

    /**
     * 提交品牌添加
     * @return  bool;
     */
    public function postbrand_add()
    {
        $input = Input::except('_token');
        $rlu = [
//            "brand_name" => "required|max:20|min:2",
//            "brand_title" => "required|max:50|min:2",
//            "brand_url" => "required|max:50|min:6",
//            "brand_thumb" => "required|max:200|min:5",
//            "brand_desc" => "required|max:150|min:5",
//            "sort_order" => "required|numeric"
        ];
        $massg = [
//            "brand_name.required" => "品牌名称不能为空",
//            "brand_name.max" => "品牌名称最多20个字符",
//            "brand_name.min" => "品牌名称最少2个字符",
//            "brand_title.required" => "品牌标题不能为空",
//            "brand_title.max" => "品牌标题最多50个字符",
//            "brand_title.min" => "品牌标题最少2个字符",
//            "brand_thumb.required" => "图片不能为空",
//            "brand_thumb.max" => "图片文件名最多200个字符",
//            "brand_thumb.min" => "图片文件名最少5个字符",
//            "brand_url.required" => "品牌网址不能为空",
//            "brand_url.max" => "品牌网址最多50个字符",
//            "brand_url.min" => "品牌网址最少6个字符",
//            "brand_desc.required" => "品牌简介不能为空",
//            "brand_desc.max" => "品牌简介最多150个字符",
//            "brand_desc.min" => "品牌简介最少5个字符",
//            "sort_order.required" => "排序不能为空请输入数字",
//            "sort_order.numeric" => "排序不能为空请输入数字",

        ];
        $validate = Validator::make($input, $rlu, $massg);
        if ($validate->passes()) {

            $rs = Brand::create($input);
            if ($rs) {
                return redirect('manager/brand');
            } else {
                return back()->with('errors', '添加失败未知错误请稍后再试');
            }
        } else {
            return back()->withErrors($validate);
        }
    }
    /**
     * 删除品牌
     * @param $id int 品牌主键
     * @return  bool;
     */
    public function brand_del($id){
        $rs = Brand::where('brand_id',$id)->delete();
        if($rs){
            return redirect('manager/brand');
        }else{
            return back()->with('errors','删除失败稍后再试');
        }
    }
    /**
     * 编辑品牌
     * @param $id int 品牌主键
     * @return  品牌编辑列表;
     */
    public function brand_edit($id){
        $data = Brand::find($id);
        //dd($data);
        return view('admin.brand.brandEdit',compact('data'));
    }
    /**
     * 提交品牌编辑
     * @param $id int 品牌主键
     * @return  bool;
     */
    public function postbrand_edit($id){
        $input = Input::except('_token');

        $rlu = [
            "brand_name" => "required|max:20|min:2",
            "brand_title" => "required|max:50|min:2",
            "brand_url" => "required|max:50|min:6",
            "brand_thumb" => "required|max:200|min:5",
            "brand_desc" => "required|max:150|min:5",
            "sort_order" => "required|numeric"
        ];
        $massg = [
            "brand_name.required" => "品牌名称不能为空",
            "brand_name.max" => "品牌名称最多20个字符",
            "brand_name.min" => "品牌名称最少2个字符",
            "brand_title.required" => "品牌标题不能为空",
            "brand_title.max" => "品牌标题最多50个字符",
            "brand_title.min" => "品牌标题最少2个字符",
            "brand_thumb.required" => "图片不能为空",
            "brand_thumb.max" => "图片文件名最多200个字符",
            "brand_thumb.min" => "图片文件名最少5个字符",
            "brand_url.required" => "品牌网址不能为空",
            "brand_url.max" => "品牌网址最多50个字符",
            "brand_url.min" => "品牌网址最少6个字符",
            "brand_desc.required" => "品牌简介不能为空",
            "brand_desc.max" => "品牌简介最多150个字符",
            "brand_desc.min" => "品牌简介最少5个字符",
            "sort_order.required" => "排序不能为空请输入数字",
            "sort_order.numeric" => "排序不能为空请输入数字",

        ];
        $validate = Validator::make($input, $rlu, $massg);
        if ($validate->passes()) {

            $rs = Brand::where('brand_id',$id)->update($input);
            if ($rs) {
                return redirect('manager/brand');
            } else {
                return back()->with('errors', '添加失败未知错误请稍后再试');
            }
        } else {
            return back()->withErrors($validate);
        }
    }



    public function uploadify()
    {

        $file = Input::file('Filedata');
        if($file -> isValid()){
            $entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
            $newName = date('YmdHis').mt_rand(100,999).'.'.$entension;
            $path = $file -> move('brands/',$newName);
            $filepath = 'brands/'.$newName;
            return $filepath;
        }

    }

    /**
     * 品牌快捷搜索
     */
    public function brand_search($word){
        $data = Brand::where('brand_name','like',"%$word%")->paginate(10);
        return view('admin.brand.brandList',compact('data'));
    }

    public function brand_lists(){
        $sql =['goods_name'=> '韩国美女',
                'goods_sn'=>'201605119830316',
                'cat_id'=>'23',
                'goods_brand'=>'8',
                'goods_unit'=>'9',
                'goods_number'=>'1',
                'market_price'=>'11',
                'shop_price'=>'11',
                'goods_desc'=>'<h1 style="font-size:16px;font-family:\'Microsoft Yahei\';color:#333333;background-color:#FFFFFF;">
                正品 2014春装新款 女 绣花针织衫 开衫外套浮桑初 蓝色</h1><h1 style="font-size:16px;font-family:\'Microsoft Yahei\';color:#333333;background-color:#FFFFFF;">正品 2014春装新款 女 绣花针织衫 开衫外套浮桑初 蓝色</h1>',
                'goods_ori'=>'uploads/20160511/20160511083801773.jpg',
                'add_time'=>'1462955882',
                'is_new'=>'1',
                'is_best'=>'0',
                'is_hot'=>'0',
        ];

        for($i = 1;$i<=100;$i++){
            echo $i;
            $sql['cat_id']=rand(28,100);
            $sql['goods_sn']+=1;
            $sql['add_time']+=1;
            $a = (new Goods())->insert($sql);
            if($a){
                echo "ok",'<br>';
            }else{
                echo 'no';
            }
        }

    }
    public function brandss(){

        $sql = [

            'brand_title'=>'测试品牌',
            'brand_url'=>'www.baidu.com',
            'brand_thumb'=>'brands/20160511091947487.jpg',
            'brand_desc'=>'测试品牌测试品牌',
            'sort_order'=>'',


        ];
        $a = new Brand();
        for($i =1 ; $i<=50; $i++){
        $sql['brand_name'] = '测试品牌'.rand(1000,9999);
            if( $a->insert($sql)){
                echo $i;
            }else{
                echo 'no';
            }
        }

    }
}
